define(['moment'], function (moment) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config) {

            $scope.init = function(){
                $scope.user_name = config.cache.name;
                $scope.envInfo = $filter('translate')('Login.env.'+config.setting.environment);
                // 預設 設備狀態
                $scope.mode = 'M';
                $scope.is_offline = false;

                // 自動隱蔽鍵盤
                element_barcode_no = document.getElementById('input_barcode');
                makeBarcodeInput(element_barcode_no);
                element_barcode_no.focus();
            };

            $scope.back = function(){
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('menu', true);
            };

            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        input_barcode_no_callback(result.text);
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };

            // barcode輸入
            $scope.input_barcode = function(barcode){
                input_barcode_no_callback(barcode);
            };

            // 轉換狀態資料清空
            $scope.ChangeTab = function(mode){
                $scope.mode = mode;

                // 清除設備
                $scope.machine_no = '';

                // 清除台車
                $scope.mfu_no = '';
                $scope.mfu_acc_count = '';
                $scope.mfu_usage_count = '';
                $scope.mfu_last_maintain_date = '';

                // 清除 feeder
                $scope.feeder_no = '';
                $scope.feeder_acc_count = '';
                $scope.feeder_usage_count = '';
                $scope.feeder_last_maintain_date = '';

                // 清除 下料明細資料
                $scope.details = [];
                element_barcode_no.focus();
            }
            // barcode輸入 callback
            function input_barcode_no_callback(barcode){
                //每一查詢就清空之前的查詢
                $scope.machine_no = ''; 
                $scope.mfu_no = '';
                $scope.feeder_no = '';
                $scope.mfu_acc_count = '';
                $scope.feeder_acc_count = '';
                $scope.mfu_usage_count = '';
                $scope.feeder_usage_count = '';
                $scope.mfu_last_maintain_date = '';
                $scope.feeder_last_maintain_date = '';
                if(barcode != ''){
                    // 設備下線
                    if($scope.mode == 'M'){
                        machine_info_get(barcode, function(itemList){
                            if(itemList && itemList.length > 0){
                                // 判斷此設備是否有上料紀錄
                                // 已上料 40
                                if(itemList[0].state == '40'){
                                    $scope.area_no = itemList[0].area_no;
                                    $scope.machine_no = itemList[0].equipment_no;
                                    var content = {
                                        area_no: $scope.area_no,
                                        equipment_no: $scope.machine_no
                                    };
                                    reel_down_set_process(content,function(){
                                        $scope.$root.showToast($filter('translate')('SMT04.msg.success'));
                                        var query_condition_info = [
                                            {column_name:'ResourceAction', query_mode:'0', value: '0', merge_condition_model:'AND'},
                                            {column_name:'EquipmentNo', query_mode:'0', value: $scope.machine_no, merge_condition_model:'AND'}
                                        ];
                                        // 取得 下料明細資料
                                        action_log_info_get(query_condition_info, function(itemList){
                                            if(itemList && itemList.length > 0){
                                                $scope.details = itemList;
                                            } else {
                                                $scope.details = [];
                                            };
                                        });
                                    });
                                } else {
                                    $scope.$root.showAlert($filter('translate')('SMT04.msg.machine_no_feeding_record'));
                                };
                            } else {
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        });
                    // 台車下線
                    } else if($scope.mode == 'T'){
                        mfu_info_get(barcode, function(itemList){
                            if(itemList && itemList.length > 0){
                                // 判斷此台車是否已上料 40 或 已上線 60
                                if(itemList[0].state == '40' || itemList[0].state == '60'){
                                    if(itemList[0].state != '60' && $scope.is_offline == true){//20201008 add by Olilver for M#78088 SMT下線下料功能確認
                                        $scope.$root.showAlert($filter('translate')('SMT04.msg.mfu_no_feeding_record'));
                                    }else{
                                        $scope.mfu_no = itemList[0].mfu_no;
                                        $scope.mfu_acc_count = itemList[0].acc_count;
                                        $scope.mfu_usage_count = itemList[0].usage_count;
                                        $scope.mfu_last_maintain_date = itemList[0].last_maintain_date;
                                        var content = {
                                            type: '0', // 0: 下線並下料 ; 1: 僅下線
                                            mfu_no: $scope.mfu_no
                                        };
                                        if($scope.is_offline == true){
                                            content.type = '1';
                                        }
                                        reel_down_set_process(content,function(){
                                            $scope.$root.showToast($filter('translate')('SMT04.msg.success'));
                                            var query_condition_info = [
                                                {column_name:'ResourceAction', query_mode:'0', value: '0', merge_condition_model:'AND'},
                                                {column_name:'MFUNo', query_mode:'0', value: $scope.mfu_no, merge_condition_model:'AND'}
                                            ];
                                            action_log_info_get(query_condition_info, function(itemList){
                                                if(itemList && itemList.length > 0){
                                                    $scope.details = itemList;
                                                } else {
                                                    $scope.details = [];
                                                };
                                            });
                                        });
                                    }
                                } else {//state不等於40也不等於60時
                                    $scope.$root.showAlert($filter('translate')('SMT04.msg.mfu_no_no_online_feeding_record'));
                                };
                            } else {
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        });
                    // Feeder 下線
                    } else if($scope.mode == 'F'){
                        var content = {
                            feeder_no: barcode
                        }
                        feeder_info_get(content,function(data){
                            // 判斷此 Feeder是否已上料 40 或 已上線 60
                            if(data.state == '40' || data.state == '60' || data.state == '50'){
                                if(data.state != '60' && $scope.is_offline == true){//20201008 add by Olilver for M#78088 SMT下線下料功能確認
                                    $scope.$root.showAlert($filter('translate')('SMT04.msg.feeder_no_online_record'));
                                }else{
                                    $scope.feeder_no = data.feeder_no;
                                    $scope.feeder_acc_count = data.acc_count;
                                    $scope.feeder_usage_count = data.usage_count;
                                    $scope.feeder_last_maintain_date = data.last_maintain_date;
                                    var content = {
                                        type: '0', // 0: 下線並下料 ; 1: 僅下線
                                        feeder_no: $scope.feeder_no
                                    };
                                    if($scope.is_offline == true){
                                        content.type = '1';
                                    }
                                    reel_down_set_process(content,function(){
                                        $scope.$root.showToast($filter('translate')('SMT04.msg.success'));
                                    });
                                }
                            } else {//state不等於40也不等於60時
                                $scope.$root.showAlert($filter('translate')('SMT04.msg.mfu_no_no_online_feeding_record'));
                            };
                        });
                    } else {
                        $scope.$root.showAlert($filter('translate')('SMT.common.msg.process_error'));
                    };
                };
            };

            // 下料明細開窗
            $scope.loadStationNo = function () {
                $scope.$root.showQueryList({
                    key: 'page_no',
                    header: [
                        {label:$filter('translate')('SMT04.queryList.resource_no'), code:'page_no'},
                        {label:$filter('translate')('SMT04.queryList.resource_name'), code:'page_name'}
                    ],
                    query: function(conditions, feedback){
                        feedback($scope.details);
                    }
                });
            };

            // 取得 設備資料
            function machine_info_get(machine_no, callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : 'smt_equipment_info_get',
                    query_condition_info:[
                        {column_name:' a.EquipmentNo', query_mode:'0', value: machine_no, merge_condition_model:'AND'},
                    ]
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            };

            // 下料作業
            function reel_down_set_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.reel_down_set_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 取得 台車資料
            function mfu_info_get(mfu_no, callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : 'mfu_info_get',
                    query_condition_info:[
                        {column_name:'MFUNo', query_mode:'0', value: mfu_no, merge_condition_model:'AND'},
                    ]
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            };

            // 帶出 Feeder資訊
            function feeder_info_get(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.feeder_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 點擊下料明細 取得下料資料
            function action_log_info_get(query_condition_info, callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : 'action_log_info_get',
                    query_condition_info: query_condition_info
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            };

            $scope.input_PDA_barcode = function(event){
                if (event.keyCode == 13) {
                    element_barcode_no.blur();
                    input_barcode_no_callback($scope.barcode)
                    $scope.barcode = '';
                } else {
                    $scope.barcode = $scope.barcode || '';
                }
            }

            //20200914 modify by Dustdusk for M#: 增加
            function makeBarcodeInput(el) {
                var _input = el.getElementsByClassName('barcode_input')[0];
                //var _mask = el.getElementsByClassName('barcode_mask')[0];
                el.focus = function () {
                    //e.preventDefault();
                    //_mask.click();
                    el.click();
                }
                el.addEventListener('click', function (e) {
                    e.preventDefault();
                    //_input.value = '';
                    if (document.activeElement !== _input)
                        _input.focus();
                });
                _input.addEventListener('focus', function (e) {
                    e.preventDefault();
                    setTimeout(function () {
                        _input.removeAttribute('readonly');
                    }, 200);
                });
                _input.addEventListener('blur', function (e) {
                    e.preventDefault();
                    _input.setAttribute('readonly', 'readonly');
                });
            }
        }
    ];
});
